//
//  ViewController.swift
//  加法计算器
//
//  Created by hanbo on 2018/11/27.
//  Copyright © 2018 hanbo. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var numText1: UITextField?
    var numText2: UITextField?
    var resultLabel: UILabel?
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupUI()
    }
    
    func setupUI() -> () {
        // 1. 两个 textField
        let tf1 = UITextField(frame: CGRect(x: 20, y: 100, width: 100, height: 30))
        tf1.borderStyle = .roundedRect
        tf1.text = "0"
        view.addSubview(tf1)
        
        let tf2 = UITextField(frame: CGRect(x: 140, y: 100, width: 100, height: 30))
        tf2.borderStyle = .roundedRect
        tf2.text = "0"
        view.addSubview(tf2)
        
        numText1 = tf1
        numText2 = tf2
        
        // 2. 三个label
        let l1 = UILabel(frame: CGRect(x: 120, y: 100, width: 20, height: 30))
        l1.text = "+"
        l1.textAlignment = .center
        view.addSubview(l1)
        
        let l2 = UILabel(frame: CGRect(x: 240, y: 100, width: 20, height: 30))
        l2.text = "="
        l2.textAlignment = .center
        view.addSubview(l2)
        
        let l3 = UILabel(frame: CGRect(x: 240, y: 100, width: 60, height: 30))
        l3.text = "0"
        l3.textAlignment = .right
        view.addSubview(l3)
        
        resultLabel = l3
        
        // 3.一个button
        let btn = UIButton()
        btn.setTitle("计算", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.sizeToFit()
        btn.center = view.center
        btn .addTarget(self, action: #selector(calac), for: .touchUpInside)
        view.addSubview(btn)
    }
    
    /// 计算结果
    @objc func calac()  {
        print(#function)
        
//        print("\(numText1?.text) ~~~ \(numText2?.text)")
        
        guard let num1 = Int(numText1?.text ?? ""),
              let num2 = Int(numText2?.text ?? "") else {
                print(" 必须都输入数字才能计算")
            return
        }
        
        // 处理结果
        resultLabel?.text = "\(num1 + num2)"
        
    }


}

